package com.dementors.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dementors.R;
import com.dementors.model.Dress;
import com.dementors.utils.TimeUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;


/**
 * ***********************
 * author:raochen
 * time:2015/12/1.
 * name:童装列表的适配器
 * step:
 */

public class DressAdapter extends RecyclerView.Adapter<DressAdapter.HeadLineViewHolder> {
    private Context mContext;
    private List<Dress.DressShow> mList;
    private onItemClickListener listener;
    public interface onItemClickListener{
        void itemClick(View view,int position);
    }
    public  void setOnItemClickListener(onItemClickListener listener){
        this.listener=listener;
    }
    public DressAdapter(Context context){
        this.mContext=context;
        mList=new ArrayList<>();
    }

    public void setList( List<Dress.DressShow> List){
        this.mList.clear();
        this.mList.addAll(List);
    }


    @Override
    public HeadLineViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        HeadLineViewHolder holder = new HeadLineViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_list_dress,parent,false),listener);
        return holder;
    }

    @Override
    public void onBindViewHolder(HeadLineViewHolder holder, int position) {

        Dress.DressShow  show= mList.get(position);
        holder.tv_dress_list_buyinfo.setText(show.buying_info);
        Log.i("oye", "刷新数据"+show.buying_info);
        holder.tv_dress_list_mj_promotion.setText(show.mj_promotion);
        holder.tv_dress_list_time.setText("剩" + TimeUtils.getDayCount(show.gmt_begin, show.gmt_end) + "天");
        holder.tv_dress_list_promotion.setText(show.promotion);
        holder.tv_dress_list_title.setText(show.title);
        if (!"".equals(show.main_img)) {

            Picasso.with(mContext).load(show.main_img).placeholder(R.mipmap.default_640_640).into(holder.iv_dress_list_pic);
        }
        if (!"".equals(show.logo)) {
            Picasso.with(mContext).load(show.mj_icon).into(holder.iv_dress_list_icon);
        }
    }

    @Override
    public int getItemCount() {

        return mList==null?0:mList.size();
    }



    class HeadLineViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView tv_dress_list_promotion;
        TextView tv_dress_list_title;
        TextView tv_dress_list_buyinfo;
        TextView tv_dress_list_time;
        TextView tv_dress_list_mj_promotion;
        ImageView iv_dress_list_icon;
        ImageView iv_dress_list_pic;
        private onItemClickListener listener;
        public HeadLineViewHolder(View itemView,  onItemClickListener listener) {
            super(itemView);
            this.listener=listener;
            tv_dress_list_buyinfo = (TextView) itemView.findViewById(R.id.tv_dress_list_buyinfo);
            tv_dress_list_promotion = (TextView) itemView.findViewById(R.id.tv_dress_list_promotion);
            tv_dress_list_title = (TextView) itemView.findViewById(R.id.tv_dress_list_title);
            tv_dress_list_time = (TextView) itemView.findViewById(R.id.tv_dress_list_time);
            tv_dress_list_mj_promotion = (TextView) itemView.findViewById(R.id.tv_dress_list_mj_promotion);
            iv_dress_list_pic = (ImageView) itemView.findViewById(R.id.iv_dress_list_pic);
            iv_dress_list_icon = (ImageView) itemView.findViewById(R.id.iv_dress_list_icon);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            listener.itemClick(view, getPosition());
        }
    }
}
